package com.zebra.register;

import com.zebra.remoting.config.BasisCommandCode;
import com.zebra.remoting.config.PingMessage;
import com.zebra.remoting.protocol.ZebraCommand;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * Heart beat triggerd.
 * 
 * @author sk
 */
@Sharable
public class RegisterHeartHandler extends ChannelDuplexHandler {

    private ZebraCommand ping = new ZebraCommand();
    private PingMessage hmsg = new PingMessage();
    /**
     *
     * @see io.netty.channel.ChannelInboundHandlerAdapter#userEventTriggered(ChannelHandlerContext, Object)
     */
    @Override
    public void userEventTriggered(final ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            ping.setCmd(BasisCommandCode.PING.cmd());
            ping.setLogs(true);
            ping.setResponse(hmsg);
            ctx.writeAndFlush(ping);
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }
}
